package com.xmb.wechat.util;

import android.text.TextUtils;
import android.widget.ImageView;

import com.blankj.utilcode.util.ScreenUtils;
import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.picasso.PicassoCircularTransform;
import com.xmb.wechat.picasso.PicassoSizeFitTransform;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/23 11:32
 */
public class PicLoadUtils {

    /**
     * 加载圆形图片
     *
     * @param srcType
     * @param avatar
     * @param iv
     */
    public static void setupRoundPicIntoImageView(int srcType, String avatar, ImageView iv) {
        if (TextUtils.isEmpty(avatar)) {
            Picasso.get().load(R.drawable.default_image).transform(new PicassoCircularTransform()).into(iv);
        } else {
            switch (srcType) {
                case Constant.SourceType.AVATAR_SRC_SDCARD:
                    Picasso.get().load("file://" + avatar).error(R.drawable.default_image)
                            .transform(new PicassoCircularTransform())
                            .into(iv);
                    break;
                case Constant.SourceType.AVATAR_SRC_APP_INNER_ASSETS:
                    String imgUrl = "file:///android_asset/avatars/" + avatar;
                    Picasso.get().load(imgUrl).error(R.drawable.default_image)
                            .transform(new PicassoCircularTransform())
                            .into(iv);
                    break;
                case Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE:
                    Picasso.get().load(Integer.parseInt(avatar)).error(R.drawable.default_image)
                            .transform(new PicassoCircularTransform())
                            .into(iv);
                    break;
                default:
                    Picasso.get().load(avatar).error(R.drawable.default_image)
                            .transform(new PicassoCircularTransform())
                            .into(iv);
                    break;
            }
        }
    }

    /**
     * 加载圆形头像
     *
     * @param srcType
     * @param avatar
     * @param iv
     */
    public static void setupRoundAvatarIntoImageView(int srcType, String avatar, ImageView iv) {
        if (TextUtils.isEmpty(avatar)) {
            Picasso.get().load(R.drawable.ic_wx_default_avatar).transform(new PicassoCircularTransform()).into(iv);
        } else {
            switch (srcType) {
                case Constant.SourceType.AVATAR_SRC_SDCARD:
                    Picasso.get().load("file://" + avatar).error(R.drawable.ic_wx_default_avatar)
                            .transform(new PicassoCircularTransform())
                            .into(iv);
                    break;
                case Constant.SourceType.AVATAR_SRC_APP_INNER_ASSETS:
                    String imgUrl = "file:///android_asset/avatars/" + avatar;
                    Picasso.get().load(imgUrl).error(R.drawable.ic_wx_default_avatar)
                            .transform(new PicassoCircularTransform())
                            .into(iv);
                    break;
                case Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE:
                    Picasso.get().load(Integer.parseInt(avatar)).error(R.drawable.ic_wx_default_avatar)
                            .transform(new PicassoCircularTransform())
                            .into(iv);
                    break;
                default:
                    Picasso.get().load(avatar).error(R.drawable.ic_wx_default_avatar)
                            .transform(new PicassoCircularTransform())
                            .into(iv);
                    break;
            }
        }
    }

    //加载本地存储的图片
    public static void loadSdcardPic(String pic, ImageView imageView) {
        loadSdcardPic(pic, R.drawable.default_image, imageView);
    }


    /**
     * 加载本地存储的图片
     *
     * @param pic
     * @param errorPic
     * @param imageView
     */
    public static void loadSdcardPic(String pic, int errorPic, ImageView imageView) {
        if (TextUtils.isEmpty(pic))
            Picasso.get().load(errorPic).transform(new PicassoSizeFitTransform(ScreenUtils.getScreenWidth())).into(imageView);
        else
            Picasso.get().load("file:" + pic).transform(new PicassoSizeFitTransform(ScreenUtils.getScreenWidth())).error(errorPic).into(imageView);
    }

    //加载聊天背景图片
    public static void loadChatBg(String pic, ImageView imageView) {
        if (TextUtils.isEmpty(pic))
            Picasso.get().load(R.drawable.img_default_gray).transform(new PicassoSizeFitTransform(ScreenUtils.getScreenWidth())).into(imageView);
        else if (TextUtils.equals("default", pic))
            Picasso.get().load(R.drawable.img_default_gray).transform(new PicassoSizeFitTransform(ScreenUtils.getScreenWidth())).into(imageView);
        else
            Picasso.get().load("file:" + pic).transform(new PicassoSizeFitTransform(ScreenUtils.getScreenWidth())).error(R.drawable.img_default_gray).into(imageView);
    }


}
